#!/usr/bin/env python2.5
# encoding: utf-8

if not globals().has_key("esda"):
    execfile("esda.py")
    sys.exit()

init()

import random

for i in range(2):
    name = "Stream%d"%i
    
    newStream(name)
    
    # for i in range(10):
    for i in range(100):
        streams[name][50*i:50*(i+1)] = random.random()

duplicateStream("Stream0", "CopieStream0")

def filterForStream0(startTime, endTime, value):
    if value > 0.7:
        return True
    else:
        return False
    

def startTimeFilterForStream1(startTime, endTime, value):
    return startTime - 20

def endTimeFilterForStream1(startTime, endTime, value):
    return endTime + 20

def sumOperator(value1, value2):
    return value1 + value2

newFilterStream(
    "FilteredStream0", "Stream0",
    filter=filterForStream0)

newFilterStream(
    "FilteredStream1", "Stream1",
    startTimeFilter=startTimeFilterForStream1,
    endTimeFilter  =endTimeFilterForStream1)

newOperatorStream(
    "SumOfFilter1AndFilter0",
    "Stream1", "Stream0",
    sumOperator)

newUnion(
    "UnionOfStream0AndFilteredStream1",
    "Stream0", "FilteredStream1")

def aggregationStartTimeIterator(context, startTime, endTime, value):
    global aggregationStartTime
    
    aggregationStartTime = int(startTime / 200) * 200
    context['result'] = aggregationStartTime

def aggregationEndTimeIterator(context, startTime, endTime, value):
    global aggregationStartTime
    
    context['result'] = max(aggregationStartTime + 200, int(endTime / 200) * 200)

def aggregationValueIterator(context, startTime, endTime, value):
    context['result'] = value + context['result'] if context['result'] != None else value

def aggregationGrouper(startTime, endTime, value):
    return int(startTime / 200)

newAggregationStream(
    "AggregationOfUnion", "UnionOfStream0AndFilteredStream1",
    aggregationStartTimeIterator, aggregationEndTimeIterator,
    aggregationValueIterator, aggregationGrouper)

def sampleFactory():
    global sampleIteratorCurrentValue
    sampleIteratorCurrentValue = None

def sampleIterator(stream, event):
    global sampleIteratorCurrentStartTime, sampleIteratorCurrentEndTime, sampleIteratorCurrentValue
    
    if sampleIteratorCurrentValue == None:
        sampleIteratorCurrentStartTime = event[1]
        sampleIteratorCurrentEndTime   = event[2]
        sampleIteratorCurrentValue     = event[3]
        return
    
    if event[2] - sampleIteratorCurrentEndTime < 50:
        if event[2] > sampleIteratorCurrentEndTime:
            sampleIteratorCurrentEndTime = event[2]
        
        sampleIteratorCurrentValue  += event[3]
    else:
        stream[sampleIteratorCurrentStartTime:sampleIteratorCurrentEndTime] = sampleIteratorCurrentValue
        sampleIteratorCurrentStartTime = sampleIteratorCurrentEndTime
        sampleIteratorCurrentEndTime   = event[2]
        sampleIteratorCurrentValue     = event[3]
    

name = "Stream3"
newStream(name)

startTime = 0

for i in range(100):
    endTime = startTime + random.randint(1, 100)
    
    streams[name][startTime:endTime] = random.random()
    
    startTime = endTime

newIteratorStream("iteratedStream", name, sampleIterator, sampleFactory)

def sampleFactory2():
    global iter2PrevValue
    
    iter2PrevValue = None

def sampleIterator2(stream, event):
    global iter2PrevValue
    
    if iter2PrevValue == None:
        iter2PrevValue = event[3]
        currentStart = event[1]
    else:
        stepSize = (iter2PrevValue - event[3]) / 10
        currentStart = event[1]
        
        while abs(iter2PrevValue - event[3]) > 2 * abs(stepSize):
            iter2PrevValue -= stepSize
            stream[currentStart:currentStart + 1] = iter2PrevValue
            currentStart += 1
    
    stream[currentStart:event[2]] = event[3]
    iter2PrevValue = event[3]

newIteratorStream("iter2", "Stream0", sampleIterator2, sampleFactory2)

print "Created streams"
